//
//  MergeViewController.m
//  NewProject
//
//  Created by admin on 2017/6/15.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "MergeViewController.h"
#import "MergeCollectionViewCell.h"
@interface MergeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)UIView *topView;
@property (nonatomic, strong)NSArray *firstArray;
@property (nonatomic, strong)NSArray *secArray;
@property (nonatomic, strong)NSArray *thirdArray;
@property (nonatomic, strong)NSArray *forArray;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UITapGestureRecognizer *gestureRecognize;
@property (nonatomic, strong)NSMutableArray *labelArray;
@property (nonatomic, strong)UIButton *chartButton;

@end

@implementation MergeViewController
- (NSArray *)firstArray{
    if (!_firstArray) {
        _firstArray = [NSArray array];
    }
    return _firstArray;
}

- (NSMutableArray *)labelArray{
    if (!_labelArray) {
        _labelArray = [[NSMutableArray alloc]init];
    }
    return _labelArray;
}

- (NSArray *)secArray{
    if (!_secArray) {
        _secArray = [NSArray array];
    }
    return _secArray;
}

- (NSArray *)thirdArray{
    if (!_thirdArray) {
        _thirdArray = [NSArray array];
    }
    return _thirdArray;
}

- (NSArray *)forArray{
    if (!_forArray) {
        _forArray = [NSArray array];
    }
    return _forArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"凑单专区";
    self.view.backgroundColor = RGB0X(0xf1f1f1);
    
    self.firstArray = @[@{kTitle:@"地雷瓜",kImage:@"商品",kType:@"0",kPrice:@"29.9",kDetail:@"0"},@{kTitle:@"地雷瓜1个",kImage:@"商品",kType:@"0",kPrice:@"10",kDetail:@"白送"},@{kTitle:@"地雷瓜",kImage:@"商品",kType:@"0",kPrice:@"29.9",kDetail:@"0"},@{kTitle:@"地雷瓜1个",kImage:@"商品",kType:@"0",kPrice:@"10",kDetail:@"白送"},@{kTitle:@"地雷瓜",kImage:@"商品",kType:@"1",kPrice:@"29.9",kDetail:@"买一送一"},@{kTitle:@"地雷瓜1个",kImage:@"商品",kType:@"0",kPrice:@"10",kDetail:@"白送"}];
    self.secArray = @[@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},];
    self.thirdArray = @[@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},];
    self.forArray = @[@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},@{kTitle:@"",kImage:@"",kType:@"",kPrice:@"",kDetail:@""},];
    [self createCollection];
    [self createTopView];
    // Do any additional setup after loading the view.
}

- (void)createTopView{
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0,  9/WIDTH_6_SCALE, WIDTH, 40/WIDTH_6_SCALE)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_topView];
    
    UILabel * aLabel = [[UILabel alloc]init];
    aLabel.frame = CGRectMake(34/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    aLabel.font = FONT(13);
    aLabel.textColor = RGB0X(0x333333);
    aLabel.text = @"0-5元";
    [_topView addSubview:aLabel];
    [self.labelArray addObject:aLabel];
    
    UILabel * bLabel = [[UILabel alloc]init];
    bLabel.frame = CGRectMake(120/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    bLabel.font = FONT(13);
    bLabel.text = @"5-10元";
    bLabel.textColor = RGB0X(0x333333);
    [_topView addSubview:bLabel];
    [self.labelArray addObject:bLabel];
    
    UILabel * cLabel = [[UILabel alloc]init];
    cLabel.frame = CGRectMake(207/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    cLabel.font = FONT(13);
    cLabel.textColor = RGB0X(0x333333);
    cLabel.text = @"10-20元";
    [_topView addSubview:cLabel];
    [self.labelArray addObject:cLabel];
    
    UILabel * dLabel = [[UILabel alloc]init];
    dLabel.frame = CGRectMake(296/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 70/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    dLabel.font = FONT(13);
    dLabel.textColor = RGB0X(0x333333);
    dLabel.text = @"20元以上";
    [_topView addSubview:dLabel];
    [self.labelArray addObject:dLabel];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.frame = CGRectMake(20/WIDTH_6_SCALE, 43/WIDTH_6_SCALE, 65/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
    self.lineView.backgroundColor = RGB0X(0xFFD601);
    [self.view addSubview:self.lineView];

    _gestureRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureRecognizeClick:)];
    [_topView addGestureRecognizer:_gestureRecognize];

    _chartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_chartButton setImage:[UIImage imageNamed:@"购物车"] forState:UIControlStateNormal];
    [_chartButton addTarget:self action:@selector(chatButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_chartButton];
    
    [_chartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-17/WIDTH_6_SCALE);
        make.bottom.equalTo(self.view.mas_bottom).offset(-12/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(53/WIDTH_6_SCALE, 53/WIDTH_6_SCALE));
    }];
}

- (void)gestureRecognizeClick:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.topView];
    NSInteger index =  point.x/(WIDTH/4);
    
    [self.labelArray enumerateObjectsUsingBlock:^(UILabel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == index) {
            obj.textColor = RGB0X(0x272727);
        }else{
            obj.textColor = RGB0X(0x5d5d5d);
        }
    }];
                                
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.frame = CGRectMake(20/WIDTH_6_SCALE+89/WIDTH_6_SCALE*index, 43/WIDTH_6_SCALE, 65/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
    }];
}

- (void)createCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
     layout.minimumInteritemSpacing = 1;// 垂直方向的间距
     layout.minimumLineSpacing = 1; // 水平方向的间距
    layout.itemSize = CGSizeMake(187/WIDTH_6_SCALE, 268/WIDTH_6_SCALE);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50/WIDTH_6_SCALE, WIDTH, HIGHT-64-50/WIDTH_6_SCALE) collectionViewLayout:layout];
    _collectionView.backgroundColor = RGB0X(0xf1f1f1);;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:_collectionView];
}

#pragma mark --- UICollectionViewDataSource ---

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.firstArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *strIdentifier = SF(@"%ld",(long)indexPath.row);
    [collectionView registerClass:[MergeCollectionViewCell class] forCellWithReuseIdentifier:strIdentifier];
    MergeCollectionViewCell* cell = (MergeCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:strIdentifier forIndexPath:indexPath];
    cell.model = self.firstArray[indexPath.row];
    return cell;
}

- (void)chatButtonClick{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
